<?php

class Admin extends Controller
{
	
	function __construct()
	{
		parent::Controller();
		$this->quickauth->restrict('admin');
		$this->load->model('pages_admin_model');
	}
	
	function Admin()
	{
		self::__construct();
	}
	
	function index()
	{	
		
		$pages['list'] = $this->pages_admin_model->list_pages();

        foreach ($pages['list'] as $var => $value)
		{
			$title = $value['title'];
			$pages['front_navigation'][$title] = $this->pages_admin_model->get_front_navigation($value['route_id']);
            $pages['navigation_order'][$title] = $this->pages_admin_model->get_navigation_order($value['route_id']);
		}

		$data = array('body_id' => 'pages');

		$this->load->view('header', $data);
		$this->load->view('manage_pages', $pages);
		$this->load->view('footer');
	}
	
	function add()
	{
        $this->form_validation->set_rules('navigation_order', 'Navigation Order', 'numeric');
		if($this->form_validation->run('edit_page_admin'))
		{
			$link = strtolower($this->input->post('page_link'));
            $navigation_order = $this->input->post('navigation_order');

            if ($this->input->post('front_nav') == "yes")
            {
                $front_nav = 1;
            }
            else
            {
                $front_nav = 0;
            }

			$route_data = array(
								'fake_route' => $link,
                                'title' => $this->input->post('page_title'),
                                'navigation_order' => $navigation_order,
                                'front_nav' => $front_nav,
								'real_route' => 'pages/index/'.$link
							);
					
			$this->db->insert('routes', $route_data);
			
			$route1 = $this->pages_admin_model->get_route($link);
			
			$page_data = array(
								'link' => $link,
								'title' => $this->input->post('page_title'),
								'content' => $this->input->post('page_content'),
								'route_id' => $route1['id']
								);
			
			$this->db->insert('pages', $page_data);

			
			$recent = array(
							'username' => $_SESSION['username'],
							'module' => 'pages',
							'action' => 'Added page '.$link
							);
							
			$this->db->insert('recent', $recent);
			
			
			$data = array('body_id' => 'pages');
			
			$data2 = array(
							'title' => 'Page Updated',
							'message' => 'Congratulations, the page has now been edited.'
						);
			
			$this->load->view('header', $data);
			$this->load->view('message', $data2);
			$this->load->view('footer');
		} // if
		else
		{
			$id = $this->uri->segment('4');
			$page_data = $this->pages_admin_model->get_page($id);

			$data = array('body_id' => 'pages');

			$this->load->view('header', $data);
			$this->load->view('add_page', $page_data);
			$this->load->view('footer');
		} // else
	} // add
	
	function edit()
	{

        $this->form_validation->set_rules('navigation_order', 'Navigation Order', 'numeric');

		if($this->form_validation->run('edit_page_admin'))
		{
			$link = strtolower($this->input->post('page_link'));
			$id = $this->uri->segment('4');
            $navigation_order = $this->input->post('navigation_order');

            if ($this->input->post('front_nav') == "yes")
            {
                $front_nav = 1;
            }
            else
            {
                $front_nav = 0;
            }

            $route1 = $this->pages_admin_model->get_route($link);

			$route_data = array(
								'fake_route' => $link,
                                'title' => $this->input->post('page_title'),
                                'navigation_order' => $navigation_order,
                                'front_nav' => $front_nav,
								'real_route' => 'pages/index/'.$link
							);
			
			$this->db->where('id', $route1['id']);			
			$this->db->update('routes', $route_data);
			
			$page_data = array(
								'link' => $link,
								'title' => $this->input->post('page_title'),
								'content' => $this->input->post('page_content'),
								'route_id' => $route1['id']
								);
			
			$this->db->where('id', $this->uri->segment('4'));
			$this->db->update('pages', $page_data);
			
			$recent = array(
							'username' => $_SESSION['username'],
							'module' => 'pages',
							'action' => 'Updated page '.$link
							);
							
			$this->db->insert('recent', $recent);
			
			
			$data = array('body_id' => 'pages');
			
			$data2 = array(
							'title' => 'Page Updated',
							'message' => 'Congratulations, the page has now been edited.'
						);
			
			$this->load->view('header', $data);
			$this->load->view('message', $data2);
			$this->load->view('footer');
		} // if
		else
		{
			$id = $this->uri->segment('4');
			$page_data = $this->pages_admin_model->get_page($id);
            $title = $page_data['title'];
            $page_data['front_navigation'] = $this->pages_admin_model->get_front_navigation($page_data['route_id']);
            $page_data['navigation_order'] = $this->pages_admin_model->get_navigation_order($page_data['route_id']);

			$data = array('body_id' => 'pages');

			$this->load->view('header', $data);
			$this->load->view('edit_page', $page_data);
			$this->load->view('footer');
		} // else
	} // edit
	
	function delete()
	{
		$id = $this->uri->segment('4');
		
		$this->db->where('id', $id);
		$query = $this->db->get('pages');
		$result = $query->row_array();
		
		$this->db->delete('routes', array('id' => $result['route_id']));
		$this->db->delete('pages', array('id' => $id));
		
		$recent = array(
						'username' => $_SESSION['username'],
						'module' => 'pages',
						'action' => 'Deleted page '.$result['link']
						);
						
		$this->db->insert('recent', $recent);
		
		$data = array('body_id' => 'pages');
		
		$data2 = array(
						'title' => 'Page Deleted',
						'message' => 'Congratulations, the page has now been deleted.'
					);
		
		$this->load->view('header', $data);
		$this->load->view('message', $data2);
		$this->load->view('footer');
		
	} // delete
	
} // admin

/* End of file admin.php */
/* Location: modules/users/controllers/admin.php */
